package com.marco.statics.controller;

import com.google.common.collect.Lists;
import com.marco.statics.domain.MapInfo;
import com.marco.statics.domain.Point;
import com.marco.statics.service.TestDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * MapController.java
 * Create on 2017/4/11
 * Copyright(c) JD Inc 2017. All Rights Reserved.
 * <pre>
 * 功能说明：
 * </pre>
 */
@Controller
@RequestMapping("/map")
public class MapController {
    @Autowired
    private TestDataService dataService;

    @RequestMapping("/points")
    public ModelAndView init(@RequestParam(value = "colum", required = true) Integer colum,
                             @RequestParam(value = "max", required = false) Long max) {
        ModelAndView mv = new ModelAndView("map/points");
        mv.addObject("colum", colum);
        mv.addObject("max", max);
        return mv;
    }

    @RequestMapping(value = "/points/get-data")
    @ResponseBody
    public MapInfo getData(@RequestParam(value = "colum", required = true) Integer colum,
                           @RequestParam(value = "max", required = true) Double max) {
        MapInfo mapInfo = dataService.getTestData(colum);
        //获取级别数据
//        Long lMax = max == null ? mapInfo.getMax() : max;

        List<Point> lists = mapInfo.getPoints();
        Integer size = lists.size();
        List<Double> levelList = Lists.newArrayList();
        if (max!=null){
            levelList.add(max);
            levelList.add(max*75/100);
            levelList.add(max*50/100);
        }else{
            for (int i = 0; i < 20; i++) {
                Integer index = (size-1) * (100 - i * 5) / 100;
                levelList.add(Double.parseDouble(lists.get(index).getValue()[2]));
            }
        }
        mapInfo.setLevelList(levelList);
        return mapInfo;
    }

    @RequestMapping("/heatmap")
    public ModelAndView getHeatMap(@RequestParam(value = "colum", required = true) Integer colum,
                                   @RequestParam(value = "max", required = true) Long max) {
        ModelAndView mv = new ModelAndView("map/heatmap");
        MapInfo mapInfo = dataService.getHeatMapData(colum);
//        mapInfo.setMax(max);
        mv.addObject("mapInfo", mapInfo);
        mv.addObject("colum", colum);
        return mv;
    }

}
